package ChatRoom;

import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;


class Code{
	final static byte ok = 0;
	final static byte newUser = 1;
	final static byte msg = 2;
	final static byte roomIsFull = 3;
	final static byte idChkErr = 4;
	final static byte userExit = 5;
}

public class ClientLogin {

	Socket sl_client = null;
	Label l_err = null;
	Frame f_login = null;
	Label l_uid = null;
	TextField tf_uid = null;

	DataInputStream dis = null;
	DataOutputStream dos = null;

	public static void main(String[] args) {
		new ClientLogin().launchFrame();
	}

	void launchFrame() {

		l_uid = new Label("ID : ");
		l_uid.setBounds(80, 100, 20, 20);

		tf_uid = new TextField("", 20);
		tf_uid.setBounds(120, 100, 170, 20);
		tf_uid.addActionListener(new TFListener());

		l_err = new Label();
		l_err.setBounds(80, 130, 300, 20);

		f_login = new Frame();
		f_login.setBounds(200, 200, 400, 200);

		f_login.add(l_uid);
		f_login.add(tf_uid);
		f_login.add(l_err);
		f_login.add(new Label("")); // without this, upper components' layout is strange, why???

		f_login.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}

		});

		f_login.setResizable(false);
		f_login.setVisible(true);
	}

	boolean connect() {
		boolean rtn = false;
		try {
			sl_client = new Socket("127.0.0.1", 9999);
			int sCode = new DataInputStream(sl_client.getInputStream()).readInt();	//wait for server's check result
			if(sCode == Code.ok){
				rtn = true;
			}else{
				switch(sCode){
					case Code.roomIsFull:
						l_err.setText("room is full, please wait and try again.");
						break;
					default:
						break;
				}
			}
		} catch(IOException ioe){
			l_err.setText("failed to connect to server.");
		}
		return rtn;
	}

	class TFListener implements ActionListener {
		public void actionPerformed(ActionEvent ae) {
			
			if(!connect()){
				tf_uid.setText("");
				return;
			}

			TextField tf = (TextField) ae.getSource();
			String u_name = tf.getText();
			
			try {
				dis = new DataInputStream(sl_client.getInputStream());
				dos = new DataOutputStream(sl_client.getOutputStream());
				
				dos.writeUTF(u_name);	//send user id to server
				
				//wait for server's check result of user id
				int servCode = dis.readInt();
				if(servCode == Code.ok){
					f_login.setVisible(false);
					new ClientMain().launchFrame(sl_client, u_name);
				}else{
					switch(servCode){
						case Code.idChkErr:
							l_err.setText("this user name already exist! Try a new one!");
							tf_uid.setText("");
							return;
						default:
							break;
					}
				}
			} catch (IOException e1) {
				l_err.setText("can not connect to server.");
			}
		}
	}
}
